import CameraControl from "./CameraControl";
import InteractiveBase from "../LevelCommon/InteractiveBase";
import CommonConstant from "../../constant/CommonConstant";

export default class CameraRayLogic extends Laya.Script3D {
    /** 当前相机*/
    private curCamera: Laya.Camera;
    /** 当前点下 点的物体*/
    private curClickObj: Laya.Sprite3D;
    /** 点击有效间隔*/
    private clickEffOff: number = 1000;
    /** 第一次点击的时间*/
    private firstClickTime: number;
    /** 是否需要检测点击到ui上*/
    public isNeedCheckUi: boolean;

    private cameraControl: CameraControl;

    private currentScene3D: Laya.Scene3D;

    private mowner: Laya.Sprite3D;

    //射线
    private _ray: Laya.Ray;

    private point: Laya.Vector2 = new Laya.Vector2();

    private _outHitResult: Laya.HitResult = new Laya.HitResult();

    onEnable() {
        this.mowner = this.owner as Laya.Sprite3D;
        this.cameraControl = this.mowner.parent.parent.getComponent(CameraControl);
        this.currentScene3D = window.gameFacade.SceneMng.CurrentScene3D;
        this.curCamera = this.owner as Laya.Camera;
        this.InitMouseEvent();
        //射线初始化（必须初始化）
        this._ray = new Laya.Ray(new Laya.Vector3(0, 0, 0), new Laya.Vector3(0, 0, 0));
    }

    private InitMouseEvent() {
        Laya.stage.on(Laya.Event.MOUSE_DOWN, this, this.MouseHandler);
        Laya.stage.on(Laya.Event.MOUSE_UP, this, this.MouseHandler);
    }

    onDisable() {
        Laya.stage.off(Laya.Event.MOUSE_DOWN, this, this.MouseHandler);
        Laya.stage.off(Laya.Event.MOUSE_UP, this, this.MouseHandler);
    }

    private MouseHandler(e: Laya.Event) {
        if (this.cameraControl != null && this.cameraControl.isRotateCamera) {
            return;
        }

        //过滤点击到上层UI
        if (this.cameraControl != null && e.target.name != CommonConstant._touchImageName) {
            return;
        }

        let touches: Array<any> = e.touches;
        let touchCount = 0;
        if(touches == null){
            touchCount = 1;
        }else{
            touchCount = touches.length;
        }
        switch (e.type) {
            case Laya.Event.MOUSE_DOWN: {
                if (touchCount == 1) {
                    this.point.x = e.stageX;

                    this.point.y = e.stageY;
                    this.curCamera.viewportPointToRay(this.point, this._ray);

                    //拿到射线碰撞的物体
                    this.currentScene3D.physicsSimulation.rayCast(this._ray, this._outHitResult);

                    if (this._outHitResult.succeeded) {
                        let firstHitObj = this._outHitResult.collider.owner;
                        /** 记录点击的物体*/
                        this.curClickObj = firstHitObj as Laya.Sprite3D;
                        this.firstClickTime = Laya.timer.currTimer;
                    }
                }
                else {
                    this.curClickObj = null;
                }

                break;
            }
            case Laya.Event.MOUSE_UP: {
                //产生射线
                this.point.x = e.stageX;
                this.point.y = e.stageY;
                this.curCamera.viewportPointToRay(this.point, this._ray);

                //拿到射线碰撞的物体
                this.currentScene3D.physicsSimulation.rayCast(this._ray, this._outHitResult);

                //如果碰撞到物体
                if (this._outHitResult.succeeded) {
                    console.log("点击触碰到的物体：", this._outHitResult.collider.owner);
                    let firstHitObj = this._outHitResult.collider.owner as Laya.Sprite3D;
                    if (firstHitObj == this.curClickObj) {
                        /** 间隔时间*/
                        let timeOff: number = Laya.timer.currTimer - this.firstClickTime;

                        /** 点击间隔在限制内 点击生效*/
                        if (timeOff <= this.clickEffOff) {
                            let interactiveBase: InteractiveBase = firstHitObj.getComponent(InteractiveBase);
                            console.log("获取的interactiveBase", interactiveBase);
                            /** 如果点击的物体有交互脚本 进行交互*/
                            if (interactiveBase != null) {
                                interactiveBase.DoInteractive();
                            }
                        }
                    }
                }
                break;
            }
        }
    }
}